﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCB.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCB.UI.Utils;

namespace Baosight.ZNH.ZMCB.UI
{
    public partial class ZMCB15 : EFViewCmes
    {
        private MineBaseInfo _block = new MineBaseInfo();//私有化下拉列表
        //private string ActionService = null;
        //private string ActionMethod = null;

        public ZMCB15()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            initData.AddBlock(_block.GetPlanDaysType("cmes.cs.planType"));//列表区
            initData.AddBlock(_block.GetPlanYear("cmes.cs.planYear"));//列表区

            base.Initialize(initData);
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            //eiInfo.AddBlock(_block.GetYears("result"));

            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetColumns(this.grid.EIBlock.GetColumns());
            EIColumn eiColumn = new EIColumn();
            eiColumn.Name = "No";
            eiColumn.TypeLabel = "C";
            //eIBlock.AddColumn(eiColumn);
            eIBlock.SetColumn(eiColumn);
            EIRow row = new EIRow();
            row["No"] = "值";
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnInsertButtonClickPopup(eiInfo, "计划工作日管理-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock =new EIBlock("result");
            eIBlock.SetColumns(this.grid.EIBlock.GetColumns());
            EIColumn eiColumn = new EIColumn();
            eiColumn.Name = "No";
            eiColumn.TypeLabel = "C";
            //eIBlock.AddColumn(eiColumn);
            eIBlock.SetColumn(eiColumn);
  
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            row["No"] = "值";
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "计划工作日管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 点击删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (this.ViewProperty.EFGridCheckedRows[i]["fdYear"].Equals("1"))
                    {
                        this.OpenEFWindowAlert("年份为全部的记录不能被删除，请重新选择！", true);
                        return;
                    }
                }
            }
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(_block.GetPlanDaysType("cmes.cs.planType"));
            _eIInfo.AddBlock(_block.GetPlanYearContent("cmes.cs.yearContent"));
            _eIInfo.AddBlock(_block.GetPlanFutureContent("cmes.cs.futureContent"));
            _eIInfo.AddBlock(_block.GetPlanYear("cmes.cs.planYear"));
            PlanWorkday view = new PlanWorkday(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);
            //if (_dialogType.Equals("insert"))
            //{
            //    //_eIInfo.AddBlock(_block.GetYears("result"));//列表区
            //    //view.fdProfessionalplanId.Value = this.fdProfessionalplanId.Value;
            //    //view.fdProduceplanId.Value = this.fdProduceplanId.Value;
            //    //view.fdPlanYear.Value = this.fdPlanYear.Value;
            //    //view.fdPlanMonth.Value = this.fdPlanMonth.Value;
            //    //view.fdTotal.Value = view.grid.GetCheckedItems().Count ;               
            //}
            if (_dialogType.Equals("update"))
            {
                if (_eIInfo.GetBlock("result").GetRow(0)["fdType"].Equals("1"))
                {
                    view.fdItem.eBlockName = "cmes.cs.futureContent";
                    view.fdUnit.Value = "月";
                }
                else
                {
                    view.fdItem.eBlockName = "cmes.cs.yearContent";
                    view.fdUnit.Value = "天";
                }
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }
}
